<template>
  <!-- element-ui级联组件 -->
  <el-cascader
    :value="value"
    :options="options"
    :props="defaultProp"
    @change="handleChange"
  />
</template>
<script>
import { getDepartment } from '@/api/department'
import { arr2tree } from '@/utils'
export default {
  props: {
    value: {
      type: Number, // 存储的是部门的id
      required: true
    }
  },
  data() {
    return {
      options: [], // 赋值给 级联组件的options
      defaultProp: {
        label: 'name', // 要展示的字段
        value: 'id' // 要存储的字段
      }
    }
  },
  created() {
    this.loadDepartment()
  },
  methods: {
    async  loadDepartment() {
      const res = await getDepartment()
      const options = arr2tree(res.data, 0)
      this.options = options
    },
    handleChange(value) {
      console.log(value)
      const id = value[value.length - 1]
      this.$emit('input', id)
    }
  }
}
</script>
